<?php

namespace App\Http\Controllers\Cruise\Merchant;

use App\Enums\Common\CommonEnum;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\MerchantAppAuth;
use App\Http\Validate\Cruise\CruiseCheckValidate;
use App\Models\Cruise\CruiseTemplateModel;
use App\Service\Cruise\Check\CruiseCheckDetailService;
use App\Service\Cruise\Check\CruiseCheckReportService;
use App\Service\Cruise\Check\CruiseOrderService;
use App\Service\Cruise\Merchant\CruiseCheckService;
use App\Service\Cruise\Shop\CruiseShopService;
use App\Service\Kms\SelfCheck\SelfCheckReportService;
use App\Service\OpenShop\SiteLocation\AuthService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class CruiseCheckController extends BaseController
{
    /**
     * 扫描二维码信息
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function getQrCodeInfo(Request $request): JsonResponse
    {
        $this->validate($request, [
            'param1' => 'required',  //managerUserId
            'param2' => 'required'   //time()
        ]);
        //扫码用户
        $userId = MerchantAppAuth::$userId;
        $shopId = $request->header('shopId', 0);
        return $this->success(CruiseCheckService::getQrCodeInfo($shopId, $userId,
            (int)deAes128Ecb($request->param1, CruiseOrderService::CRUISE_CHECK_KEY),
            (int)deAes128Ecb($request->param2, CruiseOrderService::CRUISE_CHECK_KEY)));
    }


    /**
     * 获取巡检报告列表
     * @param Request $request
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function list(Request $request): JsonResponse
    {
        $shopId       = $request->header('shopId', 0);
        $pageSize     = $request->get('pageSize', 10);
        $pageNum      = $request->get('pageNum', 1);
        $status       = $request->get('status', '');
        $templateType = $request->get('templateType', CruiseTemplateModel::TYPE_3);

        $data = CruiseCheckService::getCheckDetailList($shopId, $pageSize, $pageNum, $status, $templateType);
        return $this->success($data);
    }


    /**
     * 获取巡检报告详情
     * @param Request $request
     * @param $id
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function detail(Request $request, $id): JsonResponse
    {
        return $this->success(CruiseCheckDetailService::detail($id, ['userId' => MerchantAppAuth::$userId],
            CommonEnum::PLATFORM_MERCHANT));
    }


    /**
     * 门店已有任务名称列表
     */
    public function taskNameList(Request $request): JsonResponse
    {
        $pageSize = (int)$request->get('pageSize', 10);
        $pageNum  = (int)$request->get('pageNum', 1);
        $shopId   = (int)$request->header('shopId');
        return $this->success(CruiseCheckService::taskNameList($pageSize, $pageNum, $shopId));
    }

    /**
     * 获取门店自检列表
     * @throws GuzzleException
     */
    public function getSelfCheckReportList(Request $request): JsonResponse
    {
        $status        = (string)$request->get('status', '');  // 0有意义
        $pageSize      = (int)$request->get('pageSize', 10);
        $pageNum       = (int)$request->get('pageNum', 1);
        $shopId        = (int)$request->header('shopId');
        $startDate     = (string)$request->get('startDate', '');
        $endDate       = (string)$request->get('endDate', '');
        $cruiseOrderId = (int)$request->get('cruiseOrderId', 0);
        $templateType  = CruiseTemplateModel::TYPE_8;
        $sysTemplate   = (int)$request->get('sysTemplate', 0);

        return $this->success(
            CruiseCheckService::getSelfCheckReportList(
                $shopId, $pageSize, $pageNum, $status,
                $templateType, $startDate, $endDate, $cruiseOrderId,
                $sysTemplate
            )
        );
    }

    /**
     * 二级模板
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function systemTemplate(Request $request): JsonResponse
    {
        $params = CruiseCheckValidate::systemTemplateValidate($request->all());
        return $this->success(CruiseCheckService::systemTemplate($params->templateType));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function getSelfCheckReportListV2(Request $request): JsonResponse
    {
        $params = CruiseCheckValidate::getSelfCheckReportListV2Validate(
            $request->merge(["shopId" => $request->header("shopId")])->all()
        );
        return $this->success(CruiseCheckService::getSelfCheckReportListV2($params));
    }

    /**
     * 报告详情（提交后） - cruiseOrderId
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function afterSubmitDetail(Request $request, int $id): JsonResponse
    {
        return $this->success(SelfCheckReportService::detail($id, MerchantAppAuth::$userId, 'merchant'));
    }
}
